import Vue from 'vue'
import App from './App'


// 全局挂载store
import store from './store'

Vue.prototype.$store = store


// 全局封装、挂载toast
Vue.prototype.showToast = config => {
    let option = config
    option.icon = config.icon || 'none'
    option.position = config.icon == 'success' ? '' : (config.position || 'bottom')
    option.mask = false // 开启遮罩层
    option.duration = 3000

    uni.showToast(option)
}


// 全局挂载微信小程序分享
import wxShare from '@/utils/wxShare.js'

Vue.mixin(wxShare)

// 全局挂载微信小程序登录js
import wxMiniLogin from '@/utils/wxMiniLogin.js'

Vue.mixin(wxMiniLogin)

// 全局挂载网络请求工具
import {http} from '@/utils/luch-request/index.js'

Vue.prototype.$http = http

// 全局引入uView ui库
import uView from '@/uni_modules/uview-ui'

Vue.use(uView)

// 全局引入自定义加载动画
import LoadingPanel from '@/components/LoadingPanel'

Vue.component('LoadingPanel', LoadingPanel)

// 隐私协议
import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue'

Vue.component("privacy-popup", PrivacyPopup);

import VueCompositionAPI from '@vue/composition-api'

Vue.use(VueCompositionAPI)

// 引入时间工具方法
import {
    formatTimestamp,
    formatTimestampTime
} from '@/utils/time'

Vue.config.productionTip = false

App.mpType = 'app'
//数字千分位
Vue.filter('unitConverter', (value) => {
    if (!value) return 0
    // 获取整数部分
    const intPart = Math.trunc(value)
    // 整数部分处理，增加,
    const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    // 预定义小数部分
    let floatPart = ''
    // 将数值截取为小数部分和整数部分
    const valueArray = value.toString().split('.')
    if (valueArray.length === 2) { // 有小数部分
        floatPart = valueArray[1].toString() // 取得小数部分
        return intPartFormat + '.' + floatPart
    }
    return intPartFormat + floatPart
})

Vue.mixin({
    computed: {
        areaInfo() {
            return store.state.areaInfo || null
        },
        areaId() {
            return this.areaInfo ? this.areaInfo.areaId : ''
        },
        refreshFlag() {
            return this.$store.state.refreshFlag
        },

        userInfo() {
            return store.state.userInfo
        },
        imgUrl() {
            return store.state.imgUrl
        },
        buildImgurl() {
            return 'https://www.yinyaoit.com/'
        },
        moduleBackgroundList() {
            return this.$store.state.moduleBackgroundList
        },
        // 微信小程序，状态栏高度
        statusBarHeight() {
            return this.$store.state.systemConfig.statusBarHeight || 30// 32是默认值，单位为px
        },
        // 微信小程序，胶囊按钮高度
        menuButtonHeight() {
            return this.$store.state.systemConfig.menuButtonHeight || 32// 32是默认值，单位为px
        },
    },
    methods: {
        // 获取模块背景图
        findeModuleBackgroundUrl(moduleName) {
            const index = this.moduleBackgroundList.findIndex(el => el.module == moduleName)

            if (index != -1) {
                return this.moduleBackgroundList[index].cover
            } else {
                // return require('@/static/images/img-bg-default.png')
                return ''
            }
        },
        // 图片预览
        previewImageHandler(current, imageList) {
            uni.previewImage({
                current,
                urls: imageList
            })
        },
        // 动态设置顶部状态栏标题
        setNavigationBarTitle(title) {
            uni.setNavigationBarTitle({
                title
            })
        },
        // 格式化时间戳
        formatTimestamp(ts, formatType, isCN = false, concat = '-') {
            return formatTimestamp(ts, formatType, isCN, concat)
        },
        // 格式化时间戳
        formatTimestampTime(ts, formatType, isCN = false, concat = '-') {
            return formatTimestampTime(ts, formatType, isCN, concat)
        },
    }
})

const app = new Vue({
    ...App,
    store,
    template: '<div><Toast/><App/></div>'
})
app.$mount()
